﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sysgeprava.BL;
using Sysgeprava.Entities;
using Microsoft.Practices.Unity;

namespace Sysgeprava.Web.Controllers.Mantenimiento
{
    public class UbigeoController : Controller
    {
        [Dependency]
        public IUbigeoBL ubigeoBL { get; set; }
        //
        // GET: /Ubigeo/

        public ActionResult Index()
        {
            var lista = ubigeoBL.GetAllFromUbigeo();
            return View(lista);
        }

        [HttpPost]
        public ActionResult Index(string criterio = "")
        {
            var lista = ubigeoBL.GetAllFromUbigeoByCriterio(criterio);
            return View(lista);
        }

        public ActionResult Create()
        {
            var ubigeo = new UBIGEO();
            return View(ubigeo);
        }

        [HttpPost]
        public ActionResult Create(UBIGEO _ubigeo)
        {
            var ubigeo = new UBIGEO();

            try
            {
                ubigeo.CodUbigeo = _ubigeo.CodUbigeo.ToUpper();
                ubigeo.Departamento = _ubigeo.Departamento.ToUpper();
                ubigeo.Distrito = _ubigeo.Distrito.ToUpper();
                ubigeo.Provincia = _ubigeo.Provincia.ToUpper();
                ubigeoBL.AddUbigeo(ubigeo);

                //TempData["AlertMessage"] = "Registro Correcto";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                msg = "Ubigeo ya Registrado";
                TempData["AlertMessage"] = msg;
                return RedirectToAction("Create");
            }
        }

        public ActionResult Edit(Int32 id)
        {
            var ubigeo = ubigeoBL.GetAllFRomUbigeoById(id);
            return View(ubigeo);
        }

        [HttpPost]
        public ActionResult Edit(UBIGEO _ubigeo)
        {
            var ubigeo = new UBIGEO();
            try
            {
                ubigeo.Id = _ubigeo.Id;
                ubigeo.Departamento = _ubigeo.Departamento.ToUpper();
                ubigeo.Distrito = _ubigeo.Distrito.ToUpper();
                ubigeo.Provincia = _ubigeo.Provincia.ToUpper();
                ubigeoBL.UpdateUbigeo(ubigeo);

                //TempData["AlertMessage"] = "Edición Correcta";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                msg = "Ubigeo ya Registrado";
                TempData["AlertMessage"] = msg;
                return RedirectToAction("Edit");
            }
        }

        public ActionResult Delete(Int32 id)
        {
            ubigeoBL.DeleteUbigeo(id);
            return RedirectToAction("Index");
        }

        public ActionResult ValidarUbigeo(string codigo, int id = 0)
        {
            var ubigeo = ubigeoBL.getAllFromUbigeoByCodigo(codigo);
            var existe = ubigeo == null ? false : true;

            var result = new
            {
                existe = existe,
                Ubigeo = ubigeo != null ? ubigeo.UbigeoCompleto : ""
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}
